const router = require("koa-router")();
const { SuccessModel, FailModel } = require("../utils/model_adapter");
let { listData } = require("../common/list");
router.prefix("/api");

// 获取单个问卷信息
router.get("/question/:id", async (ctx, next) => {
  const { id } = ctx.params;
  await new Promise((resolve) => {
    setTimeout(() => {
      resolve(1);
    }, 1000);
  });
  ctx.body = new SuccessModel("success", {
    data: {
      id: id,
      title: "详情" + id,
      desc: "",
      js: "",
      css: "",
      isPublished: true,
      componentList: [
        {
          id: Math.random().toString().substring(-3),
          type: "QuestionInfo",
          title: "标题",
          isHidden: false,
          isLocked: false,
          props: { title: "标题段落", desc: "请输入描述" },
        },
        {
          id: Math.random().toString().substring(-3),
          type: "QuestionTitle",
          title: "标题",
          isHidden: false,
          isLocked: false,
          props: { text: "个人信息调研", level: 1, isCenter: false },
        },
        {
          id: Math.random().toString().substring(-3),
          type: "QuestionInput",
          title: "输入框",
          isHidden: false,
          isLocked: false,
          props: { title: "你的姓名", placeholder: "请输入你的姓名..." },
        },
        {
          id: Math.random().toString().substring(-3),
          type: "QuestionInput",
          title: "输入框2",
          isHidden: false,
          isLocked: false,
          props: { title: "你的年龄", placeholder: "请输入你的年龄..." },
        },
        {
          id: Math.random().toString().substring(-3),
          type: "QuestionParagraph",
          title: "段落",
          isHidden: false,
          isLocked: false,
          props: { title: "输入段落", placeholder: "请输入内容" },
        },
        {
          id: Math.random().toString().substring(-3),
          type: "QuestionRadio",
          title: "用户选择",
          isHidden: false,
          isLocked: false,
          props: {
            title: "用户选择",
            value: 1,
            isVertical: false,
            options: [
              { value: 1, label: "选项一" },
              { value: 2, label: "选项二" },
              { value: 3, label: "选项三" },
            ],
          },
        },
      ],
    },
  });
});

// 问卷列表信息
router.get("/question", async (ctx, next) => {
  let { limit, page, keyword, isStar, isDelete } = ctx.query;
  // let total = 100;

  if (!page) page = 1;
  if (!limit) limit = 10;
  const offset = parseInt((page - 1) * limit);

  let result = listData;

  if (keyword) {
    result = result.filter((item) => item.title.indexOf(keyword) > -1);
  }

  if (isStar) {
    console.log(result);
    result = result.filter((item) => item.isStar);
  }

  if (isDelete) {
    result = result.filter((item) => item.isDelete);
  }
  let total = result.length;

  result = result.slice(offset, offset + (limit - 0));

  ctx.body = new SuccessModel("success", {
    data: {
      data: result,
      total: total,
    },
  });
});

// 创建问卷信息
router.post("/question", async (ctx, next) => {
  ctx.body = new SuccessModel("success", {
    data: parseInt(Math.random().toString().substring(2)),
  });
});

// 更新标星
router.post("/question/:id", async (ctx, next) => {
  const { isStar, isDelete } = ctx.request.body;
  const { id } = ctx.params;
  const index = listData.findIndex((item) => item._id == id);
  if (index > -1) {
    if (isStar === true || isStar === false) {
      listData[index].isStar = isStar;
    }
    if (isDelete === true || isDelete === false) {
      listData[index].isDelete = isDelete;
    }
    ctx.body = new SuccessModel("success", {
      data: isStar,
    });
  } else {
    ctx.body = new FailModel("id错误", { data: null });
  }
});

// 复制问卷
router.get("/copyQuestion/:id", async (ctx, next) => {
  ctx.body = new SuccessModel("success", {
    data: {
      id: Math.random().toString().substring(2),
    },
  });
});

// 彻底删除
router.delete("/delete", async (ctx, next) => {
  let { ids = [] } = ctx.request.body;
  if (ids.length) {
    listData = listData.filter((item) => !ids.includes(item._id));
    ctx.body = new SuccessModel("success", {
      data: listData,
    });
  } else {
    ctx.body = new FailModel("ids错误", { data: null });
  }
  ctx.body = new SuccessModel("success", {
    data: {
      id: Math.random().toString().substring(2),
    },
  });
});

// 用户注册
router.post("/user/register", async (ctx, next) => {
  const { username, pwd, nickname, surepwd } = ctx.request.body;
  ctx.body = new SuccessModel("success", { data: true });
});

// 用户登录
router.post("/user/login", async (ctx, next) => {
  const { username, pwd } = ctx.request.body;
  ctx.body = new SuccessModel("success", { data: { token: "123kjjkaddjkjk" } });
});

// 用户信息
router.get("/user/userInfo", async (ctx, next) => {
  ctx.body = new SuccessModel("success", {
    data: { username: "happy龙", nickname: "happy" },
  });
});

var saveInfo = {};
// 保存问卷
router.post("/save/:id", async (ctx, next) => {
  const { id } = ctx.params;
  saveInfo[id] = ctx.request.body;
  ctx.body = new SuccessModel("success", { data: 1 });
});

module.exports = router;
